
#include "ControllerHunter.h"


using namespace cgGame;
using namespace cgMath;


CControllerHunter::CControllerHunter()
:CController()
{

	m_pPerception = CPerceptionFactory::CreatePerception(Character_Type_Hunter);
	m_pGameSpate = CGameState::GetGameStateInstance();

	//m_fireTime = 0.0f;
	//m_fireTimeInterval = 1.0f;
	m_isSpaceKeyUp = true;
}



CControllerHunter::~CControllerHunter()
{
	if (NULL != m_pPerception)
	{
		delete m_pPerception;
	}
	m_pPerception = NULL;
}



//CAction CControllerHunter::CalcAction()
void CControllerHunter::CalcAction(float timeDelta)
{
	m_pPerception->SelectTarget();

	cgVector2D currPos = m_pPerception->GetMyself()->GetObjPos();
	cgVector2D destPos = currPos;
	//if (NULL != m_pPerception->GetTarget())
	//{
	//	destPos = m_pPerception->GetTarget()->GetObjPos();
	//}

	////do some find-path operation
	////here just testing the program
	//if(currPos != destPos)
	//{
	//	if (currPos.x < destPos.x)
	//		currPos.x ++;
	//	if (currPos.x > destPos.x)
	//		currPos.x --;
	//	if (currPos.y < destPos.y)
	//		currPos.y ++;
	//	if (currPos.y > destPos.y)
	//		currPos.y --;
	//}

	
	//m_nextAction = CAction(currPos);
	////return CAction(currPos);//,cgVector2D(0.0f, 0.0f));

	//m_fireTime -= timeDelta;
	m_pGameSpate->UpdateHunterFireTime(timeDelta);
	HandleInput(destPos);
	//cgMath::ChangeToNearestGridPos(destPos);

	m_pPerception->PerceiveSurroundings();
	if (true == m_pPerception->IsForbiddenPos(destPos))
	{
		m_nextAction.SetPosition(currPos);
	}
	else
	{
		m_nextAction.SetPosition(destPos);
	}
}



void CControllerHunter::HandleInput(cgVector2D& destPos)
{
	if (Game_CurrState_Lose == m_pGameSpate->GetGamecurrState())
	{
		return;
	}

	if (KEYDOWN(VK_UP))
	{
		destPos.y -= m_pPerception->GetMyself()->GetMoveSpeed();
		m_pPerception->GetMyself()->SetDirection(Character_Dir_Up);
	}
	else if (KEYDOWN(VK_RIGHT))
	{
		destPos.x += m_pPerception->GetMyself()->GetMoveSpeed();
		m_pPerception->GetMyself()->SetDirection(Character_Dir_Right);
	}
	else if (KEYDOWN(VK_DOWN))
	{
		destPos.y += m_pPerception->GetMyself()->GetMoveSpeed();
		m_pPerception->GetMyself()->SetDirection(Character_Dir_Down);
	}
	else if (KEYDOWN(VK_LEFT))
	{
		destPos.x -= m_pPerception->GetMyself()->GetMoveSpeed();
		m_pPerception->GetMyself()->SetDirection(Character_Dir_Left);
	}

	if (true == m_pPerception->GetMyself()->GetIsTurning())
	{
		cgMath::ChangeToNearestGridPos(destPos);
	}


	//if (m_fireTime <= 0)
	if (m_pGameSpate->GetHunterFireTime() <= 0)
	{
		if (KEYDOWN(VK_SPACE) )
		{
			//fire
			//MessageBox(NULL, "Test", "nothing", 0);
			m_pGameSpate->AddNewBullet(m_pPerception->GetMyself()->GetObjPos(),
										Bullet_Type_Hunter,
										m_pPerception->GetMyself()->GetDirection());
			if ( 4 <= m_pGameSpate->GetHunterBulletLevel())
			{
				m_pGameSpate->SetHunterFireTime(m_pGameSpate->GetHunterFireInterval() / 2);
				//m_pGameSpate->AddNewBullet(m_pPerception->GetMyself()->GetObjPos(),
				//	Bullet_Type_Hunter,
				//	m_pPerception->GetMyself()->GetDirection());
			}
			else
			{
				//m_fireTime = m_fireTimeInterval;
				m_pGameSpate->SetHunterFireTime(m_pGameSpate->GetHunterFireInterval());

			}
		}
	}
	//if (KEYDOWN(VK_SPACE))
	//{
	//	m_isSpaceKeyUp = false;
	//}
	//if (KEYUP(VK_SPACE))
	//{
	//	m_isSpaceKeyUp = true;
	//}
}